<?php

class Video_Plugin_Service_Youtube
{
	function getName()
	{
		return 'youtube';
	}

	function getVideoInfo($link)
	{
		$return = array();
		
		$urlInfo =  parse_url($link);

		$new_code = pathinfo($link);

		$link = preg_replace("/#!/", "?", $link);

		// get v variable from the url
		$parameters = $urlInfo["query"];

		parse_str($parameters, $data);

		$code = $data['v'];

		if ($code == "")
		{
			$code = $new_code['basename'];
		}

		if (!$code)
		{
			return NULL;
		}

		$apiLink = "http://gdata.youtube.com/feeds/api/videos/$code?alt=json";

		$curl = new Nuo_Curl();

		$response = $curl -> send($apiLink);

		if ($response == "Video not found")
		{
			return FALSE;
		}

		$data = json_decode($response, 1);

		$group = $data['entry']['media$group'];

		$return['title'] = $group['media$title']['$t'];
		$return['source_id'] = $code;
		$return['duration'] = (int)$group['yt$duration']['seconds'];
		$desc = $group['media$description']['$t'];
		$desc = substr(strip_tags($desc), 0, 250);
		$return['description'] = $desc;
		$return['thumbnail_large'] = $group['media$thumbnail'][0]['url'];

		return $return;

	}

	function getEmbedCode($item, $params)
	{
		$string = <<<THESTRING
<object width=":width" height=":height">
<param name="movie" value="http://www.youtube.com/v/:code&color1=0xb1b1b1&color2=0xcfcfcf&hl=en_US&feature=player_embedded&fs=1"/>
<param name="allowFullScreen" value="true"/>
<param name="allowScriptAccess" value="always"/>
<embed src="http://www.youtube.com/v/:code&color1=0xb1b1b1&color2=0xcfcfcf&hl=en_US&feature=player_embedded&fs=1&autoplay=:autoplay" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width=":width" height=":height" wmode="transparent" />
<param name="wmode" value="transparent" />
</object>
THESTRING;

		return strtr($string, array(
			':width' => 425,
			':height' => 344,
			':autoplay' => 0,
			':code' => $item -> source_id
		));
	}

}
